import 'package:flutter/material.dart';
import 'package:flutter_getx/view/home/view.dart';
import 'package:flutter_getx/view/mine/view.dart';
import 'package:get/get.dart';
import 'package:my_image/my_image.dart';

import 'logic.dart';
import 'state.dart';

class TabPage extends StatefulWidget {
  const TabPage({super.key});

  @override
  State<TabPage> createState() => _TabPageState();
}

class _TabPageState extends State<TabPage> {
  final TabLogic logic = Get.put(TabLogic());
  final TabState state = Get.find<TabLogic>().state;
  late PageController _pageController;

  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: state.currentIndex.value);
  }

  @override
  void dispose() {
    _pageController.dispose();
    Get.delete<TabLogic>();
    print('dispose');
    super.dispose();
  }

  // 创建页面组件
  List<Widget> _buildPages() {
    return [
      _buildPageWidget(
        name: '首页',
        icon: 'tabs/home_default',
        activeIcon: 'tabs/home_active',
        bottomBarName: '首页',
        child: const HomePage(),
      ),
      _buildPageWidget(
        name: '我的',
        icon: 'tabs/my_default',
        activeIcon: 'tabs/my_active',
        bottomBarName: '我的',
        child: const MinePage(),
      ),
    ];
  }

  // 构建页面组件
  Widget _buildPageWidget({
    required String name,
    required String icon,
    required String activeIcon,
    required String bottomBarName,
    required Widget child,
  }) {
    return child;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
          child: PageView(
            controller: _pageController,
            physics: const NeverScrollableScrollPhysics(),
            onPageChanged: (index) {
              state.currentIndex.value = index;
            },
            children: _buildPages(),
          ),
        ),
        bottomNavigationBar: Obx(
          () => BottomAppBar(
            color: Colors.white,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: List.generate(state.tabBarList.length, (index) {
                final item = state.tabBarList[index];
                final isSelected = state.currentIndex.value == index;

                return Expanded(
                  child: InkWell(
                    onTap: () {
                      state.currentIndex.value = index;
                      _pageController.animateToPage(
                        index,
                        duration: const Duration(milliseconds: 300),
                        curve: Curves.easeInOut,
                      );
                    },
                    child: Container(
                      padding: const EdgeInsets.symmetric(vertical: 8),
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Image.asset(
                            ImgRes.imagesTabs(isSelected ? item['active'] : item['default']),
                            width: 32,
                            height: 32,
                          ),
                          const SizedBox(height: 4),
                          Text(
                            item['title'],
                            style: TextStyle(
                              color: isSelected ? const Color.fromARGB(255, 85, 145, 175) : Colors.black,
                              fontSize: 12,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                );
              }),
            ),
          ),
        ));
  }
}
